﻿using System.Collections.Generic;
using System.IO;
using System.Windows.Forms;
using System.Diagnostics;

namespace GShare1._0
{
    //Will Handle the applications
    //Files
    //Folders
    //Path Creation
    //File Checking
    //Updating of file list
    //File list management - events
    public struct FileDownloadInfo
    {
        public string FilePathName;
        public string FileExtension;
        public long FileSize;

    }
    public class FileManager
    {

        private string _path = Application.StartupPath;
        private string _downloadsPath;
        private string _serverInfoPath;
        private string _savedServersPath;
        private string _uploadsPath;
        private List<FileDownloadInfo> _fileInfoList;

        public FileManager()
        {
            _downloadsPath = _path + "\\" + "Downloads";
            _serverInfoPath = _path + "\\" + "Server Info";
            _savedServersPath = _path + "\\" + "Saved Servers";
            _uploadsPath = _path + "\\" + "User Uploads";
            _fileInfoList = new List<FileDownloadInfo>();
        }

        public string UploadsPath
        {
            get { return _uploadsPath; }
            set { _uploadsPath = value; }
        }
        public List<FileDownloadInfo> FileInfoList
        {
            get { return _fileInfoList; }
            set { _fileInfoList = value; }
        }
        public string Path
        {
            get { return _path; }
            set { _path = value; }
        }
        public string  DownloadsPath
        {
            get { return _downloadsPath; }
            set { _downloadsPath = value; }
        }
        public string SavedServersPath
        {
            get { return _savedServersPath; }
            set { _savedServersPath = value; }
        }        
        public string ServerInfoPath
        {
            get { return _serverInfoPath; }
            set { _serverInfoPath = value; }
        }
        
        
        

        /// <summary>
        /// Using the application startup path checks if folders exist and creates them
        /// updates the FileInfoList
        /// </summary>
        public void CheckContainingFolder()
        {
            UpdateFileInfoList();
            if (!File.Exists(_downloadsPath))
            {
                Directory.CreateDirectory(_downloadsPath);
            }
            if (!File.Exists(_serverInfoPath))
            {
                Directory.CreateDirectory(_serverInfoPath);
            }
            if (!File.Exists(_savedServersPath))
            {
                Directory.CreateDirectory(_savedServersPath);
            }
            if (!File.Exists(_uploadsPath))
            {
                Directory.CreateDirectory(_uploadsPath);
            }

        }

        public void UpdateFileInfoList()
        {
            _fileInfoList.Clear();
            string[] downloadFileNames = Directory.GetFiles(_downloadsPath);
            //check files within Downloads
            foreach (string item in downloadFileNames)
            {
                FileDownloadInfo fileDownloadInfo = new FileDownloadInfo();
                //get the files info
                FileInfo fileInfo = new FileInfo(item);

                fileDownloadInfo.FileSize = fileInfo.Length;
                fileDownloadInfo.FilePathName = item;
                fileDownloadInfo.FileExtension = fileInfo.Extension;

                _fileInfoList.Add(fileDownloadInfo);
            }


            //Debugging only
            foreach (FileDownloadInfo item in _fileInfoList)
            {
                Debug.WriteLine(item.FilePathName);
            }
            
        }
    }
}
